package com.webcontroller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.member.bsl.BaseHandler;
import com.member.bsl.LoginHandler;
import com.member.util.DBUtil;

/**
 * Servlet implementation class Controller
 */
@WebServlet("/Controller")
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public static String ACTION_LOGIN_VIEW = "01";
	public static String ACTION_LOGIN_SUBMIT = "02";
	public static String ACTION_HOME = "03";
	public static String ACTION_CART_VIEW = "04";
	private static final String REQ_PARAM_ACTION = "action";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Controller() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			HashMap lhRequest = parseRequest(request);

			String action = request.getParameter(REQ_PARAM_ACTION);

			if (ACTION_HOME.equals(action)) {
				request.getRequestDispatcher("/jsp/main.jsp").forward(request,
						response);
			} else if (ACTION_CART_VIEW.equals(action)) {
				// TODO Forward to handler
			} else if (ACTION_LOGIN_SUBMIT.equals(action)) {
				LoginHandler handler = new LoginHandler();
				Object resp = handler.process(lhRequest);
				if ((boolean) resp) {
					request.getRequestDispatcher("/jsp/success.jsp");
				}

				// TODO Forward to handler
			} else if (ACTION_LOGIN_VIEW.equals(action)) {
				// TODO Forward to handler
			} else {
				request.getRequestDispatcher("/jsp/error.jsp").forward(request,
						response);
			}

		} catch (Exception exp) {
			request.getRequestDispatcher("/jsp/error.jsp").forward(request,
					response);
		}
	}

	private HashMap parseRequest(HttpServletRequest request) {

		HashMap lhRequest = new HashMap();
		Object[] loKeySet = request.getParameterMap().keySet().toArray();
		for (int liCtr = 0; liCtr < loKeySet.length; liCtr++) {
			String lsKey = (String) loKeySet[liCtr];
			String lsValue = request.getParameter(lsKey);
			if (lsValue != null && !lsValue.equals("")) {
				lhRequest.put(lsKey, lsValue);
			}
		}
		return lhRequest;
	}

	public void destroy() {
		try {
			DBUtil.closeConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
